import csv

with open('stocks.csv') as f:
    f_csv = csv.reader(f)
    headers = next(f_csv)
    print(headers)
    for row in f_csv:
        print(row)

from collections import namedtuple

with open('/Users/jumei/PycharmProjects/123/第六章：数据编码和处理/stocks.csv') as f:
    f_csv = csv.reader(f)
    headings = next(f_csv)
    Row = namedtuple('Row', headings)
    for f in f_csv:
        row = Row(*f)
        print(row)

headers = ['Price', 'Symbol', 'Date', 'Time', 'Change', 'Volume']
rows = [{'Symbol': 'AA', 'Price': 39.48, 'Date': '6/11/2007',
         'Time': '9:36am', 'Change': -0.18, 'Volume': 181800},
        {'Symbol': 'AIG', 'Price': 71.38, 'Date': '6/11/2007',
         'Time': '9:36am', 'Change': -0.15, 'Volume': 195500},
        {'Symbol': 'AXP', 'Price': 62.58, 'Date': '6/11/2007',
         'Time': '9:36am', 'Change': -0.46, 'Volume': 935000},
        ]
with open('stocks1.csv', 'w') as f:
    f_csv = csv.DictWriter(f, headers)  # dict格式的写入headers,并区分header
    f_csv.writeheader()
    f_csv.writerows(rows)
